--Test HUD
--By Kylegar

include("shared.lua")

local hudw = ScrW()
local hudh = ScrH()/14

local font = "ScoreboardText"
local totalXP = 0
local Experiance = "experiance"
local Level = "level"
local Xpfornextlevel = "xpfornextlevel"

function hidehud(name)
    for k, v in pairs({"CHudHealth", "CHudBattery", "CHudAmmo", "CHudSecondaryAmmo"}) do
   if name == v then return false end
   end 
   
  end
  hook.Add("HUDShouldDraw", "hidehud", hidehud) 
  

function drawhud()
	
	--The Base of the GUI
	draw.RoundedBox(2, 0, ScrH() - hudh , hudw , hudh , Color(180,180,180))
	--draw health bar
	drawhealthbox()
	--Show the Player Name
	dispplayerinfo()
	--Show the weapon stuff
	weaponinfo()
	levelinfo()
	targetline()
	outlinezombies()
	
	
	
	
	

	
end
hook.Add("HUDPaint", "drawhud", drawhud)  

function drawhealthbox()
	
	local healthbarwidth = 40
	
	--Health bar (Gotta get the Health first)
 
	local plyhppercent = LocalPlayer():Health() / LocalPlayer():GetPlayerMaxHealth()
	
	--draw.RoundedBox(1, 5 , hudh - 10 , (hudw/4 - 10) * math.Round(plyhppercent) , 5 , Color(180, 0 , 0))
	draw.RoundedBox(1, 5 , (ScrH()-hudh) + 5 , (hudw/4 - 20), 20 , Color(255, 255 , 255))
	draw.RoundedBox(1, 5 , (ScrH()-hudh) + 5 , (hudw/4 - 20)* plyhppercent , 20 , Color(180, 0 , 0))
	draw.DrawText("Health: "..math.Round((plyhppercent *100)).."%" , font , (hudw/4 - 20) +50	, (ScrH()-hudh)+5 , Color(0,0,0) ,1)
	
	--Suit bar (Only on if he has suit charg)
	local armorpercent = LocalPlayer():GetPlayerArmor() / LocalPlayer():GetPlayerMaxArmor()
	
	
	draw.RoundedBox(1, 5 , (ScrH()-hudh) + 35 , (hudw/4 - 20), 20 , Color(255, 255 , 255))
	draw.RoundedBox(1, 5 , (ScrH()-hudh) + 35 , (hudw/4 - 20)* armorpercent , 20 , Color(0, 0 , 255))
	draw.DrawText("Armor: "..(armorpercent*100).."%" , font , (hudw/4 - 20) + 50	, (ScrH()-hudh)+35 , Color(0,0,0) ,1)
	
	
	

 end 
 
 function dispplayerinfo()
	
	
	draw.DrawText("Name: "..LocalPlayer():GetName() , font , ScrW()/2, (ScrH()-hudh) +5 , Color(0,0,0), 1)
	draw.DrawText("Zombie Kills: "..LocalPlayer():Frags(), font, ScrW()/2, (ScrH() - hudh) + 20 , Color(0,0,0),1)
	draw.DrawText("Deaths: "..LocalPlayer():Deaths(), font, ScrW()/2, (ScrH() - hudh) + 35 , Color(0,0,0),1)
	draw.DrawText("Ping: "..LocalPlayer():Ping(), font, ScrW()/2, (ScrH() - hudh) + 50 , Color(0,0,0),1)
	
	
end

function weaponinfo()
	local currentweap = LocalPlayer():GetActiveWeapon()
	if !(currentweap:IsValid()) then return end
	if(currentweap:IsWeapon()) then
	 draw.DrawText(currentweap:GetPrintName() , font, ScrW() - (ScrW()/4), (ScrH() - hudh) + 5, Color(0,0,0) ,1)
	end
	 if !(currentweap:Clip1() == -1) then 
		draw.DrawText("Ammo: "..currentweap:Clip1() , font, ScrW() - (ScrW()/4), (ScrH() - hudh) + 20, Color(0,0,0) ,1)
	 end
	 if !(currentweap:Clip2() == -1) then 
		draw.DrawText("Secondary Ammo: "..currentweap:Clip2() , font, ScrW() - (ScrW()/4), (ScrH() - hudh) + 35, Color(0,0,0) ,1)
	 end
	 if (currentweap:Clip1() == 0) then
		draw.DrawText("RELOAD" , font, ScrW() - (ScrW()/4), (ScrH() - hudh) + 50, Color(255,0,0) ,1)
	 end
	
end	

function levelinfo()
	local xp = LocalPlayer():GetXP()
	local level = LocalPlayer():GetLevel()
	local xpfornextlevel = LocalPlayer():GetXPForNextLevel()
	local xppercent = xp / xpfornextlevel
	draw.RoundedBox(0 , 1, (ScrH() - hudh)- 15 , ScrW() , 15 , Color(255 , 255 , 255))
	draw.RoundedBox(0 , 1, (ScrH() - hudh)- 15 , ScrW()* xppercent , 15 , Color(0 , 255 , 0))
	draw.DrawText(xp.."/"..xpfornextlevel.." Level "..level , font , ScrW()/2 , (ScrH()-hudh) - 16 , Color(0 , 0 , 0) , 1)
end

function targetline()
	local tr = {}
	tr.start = LocalPlayer():GetPos()
	tr.endpos = LocalPlayer():GetPos() + (LocalPlayer():GetAngles():Forward() * 100)
	
	local surfself = tr.start:ToScreen()
	local surftar = tr.endpos:ToScreen()
	
	surface.DrawLine(surfself.x,surfself.y,surftar.x,surftar.y)

end

function outlinezombies()
	npcs = NPCsFindInSphere(350)
	
	for k,v in pairs(npcs) do
	 local max = v:OBBMaxs():ToScreen()
	 local min = v:OBBMins():ToScreen()
	 surface.SetDrawColor(0,255,0,255)
	 surface.DrawOutlinedRect(max.x,max.y,(max.x-min.x),(max.y-min.y))
	 surface.DrawOutlinedRect(max.x,max.y-20,40,20)
	 surface.SetTextPos(max.x+5,max.y-10)
	 surface.SetFont(font)
	 surface.SetTextColor(255,255,255,255)
	 surface.DrawText(v:GetClass())
	end

end

local counter =0 
function NPCsFindInSphere(radius)
	local npcs = ents.GetAll()
	local tabnpcs = {}
	
	
	for k,v in pairs(npcs) do
	 if(v:IsNPC()) then
	  local ppos = LocalPlayer():GetPos()
	  local npos = v:GetPos()
	  local length = ppos:Distance(npos)
	 
	  if(length<=radius) then
	   table.insert(tabnpcs,v)
	  end
	 end
	end
	counter= counter+1
	return tabnpcs
end
